﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBLibrary;

namespace AdminClient
{
    public class AdminInterface
    {
        private Administrator admin;

        /// <summary>
        /// Constructor
        /// </summary>
        public AdminInterface()
        {
            this.admin = new Administrator();
        }

        /// <summary>
        /// Print the header in the console
        /// </summary>
        private void printHeader()
        {
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("---------- Welcome to the administrator interface ----------");
            Console.WriteLine("-------- Enter help command to access the help page --------");
            Console.WriteLine("------------------------------------------------------------\n");
        }

        /// <summary>
        /// Print the help page
        /// </summary>
        private void printHelp()
        {
            Console.WriteLine("------------------------- Help page ------------------------\n");
            Console.WriteLine("rmu [id] : remove an user giving his ID");
            Console.WriteLine("rmp [id] : remove a picture giving his ID");
            Console.WriteLine("rma [id] : remove an album giving his ID");
            Console.WriteLine("quit : exit the program");
            Console.WriteLine("help : print help page");
            Console.WriteLine("------------------------------------------------------------\n");
        }

        /// <summary>
        /// Connect the admin
        /// </summary>
        private void connect()
        {
            // Get the login and password 
            //
            Console.Write("Login : ");
            String nickname = Console.ReadLine();
            Console.Write("Password : ");
            String password = Console.ReadLine();

            // Try to connext the admin
            //
            this.admin.connect(nickname, password);
            if (!admin.IsConnected) Console.WriteLine("Connection failed. The nickname and/or the password may be wrong.");
            else Console.WriteLine("Connected.");
        }

        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="command">Command to execute</param>
        private void executeCmd(String command)
        {
            int id;
            switch (command)
            {
                case "rmu" :
                    Console.Write("Enter the ID of the user : ");
                    id = int.Parse(Console.ReadLine());
                    try
                    {
                        admin.removeUser(id);
                        Console.WriteLine("Operation succeful.");
                    }
                    catch (UnfoundCustomerException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;
                case "rmp" :
                    Console.Write("Enter the ID of the picture : ");
                    id = int.Parse(Console.ReadLine());
                    try
                    {
                        admin.removeImage(id);
                        Console.WriteLine("Operation succeful.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;
                case "rma":
                    Console.Write("Enter the ID of the album : ");
                    id = int.Parse(Console.ReadLine());
                    try
                    {
                        admin.removeAlbum(id);
                        Console.WriteLine("Operation succeful.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;
                case "help":
                    printHelp();
                    break;
                case "quit":
                    Environment.Exit(0);
                    break;
                default :
                    break;
            }
        }

        /// <summary>
        /// Run the interface
        /// </summary>
        public void run()
        {
            printHeader();

            // Connect the admin
            //
            while (!admin.IsConnected)
            {
                connect();
            }

            // Wait for commands
            //
            while (true)
            {
                // Print prompt
                //
                Console.Write("> ");
                String command = Console.ReadLine();
                executeCmd(command);
            }
        }
    }
}
